// 用户管理页面功能
document.addEventListener('DOMContentLoaded', function() {
    // 搜索功能
    const searchInput = document.getElementById('searchInput');
    if (searchInput) {
        searchInput.addEventListener('input', function(e) {
            const searchTerm = e.target.value.toLowerCase();
            document.querySelectorAll('.user-row').forEach(row => {
                const rowText = row.textContent.toLowerCase();
                row.style.display = rowText.includes(searchTerm) ? '' : 'none';
            });
        });
    }

    // 重置搜索
    const clearSearchBtn = document.getElementById('clearSearch');
    if (clearSearchBtn) {
        clearSearchBtn.addEventListener('click', function() {
            if (searchInput) searchInput.value = '';
            document.querySelectorAll('.user-row').forEach(row => {
                row.style.display = '';
            });
        });
    }

    // 删除确认
    document.querySelectorAll('.delete-btn').forEach(btn => {
        btn.addEventListener('click', function(e) {
            const userName = this.getAttribute('data-user-name');
            if (!confirm(`确定要删除用户 ${userName} 吗？此操作不可恢复！`)) {
                e.preventDefault();
            }
        });
    });

    // 自动关闭消息提示
    setTimeout(() => {
        const alerts = document.querySelectorAll('.alert');
        alerts.forEach(alert => {
            alert.style.display = 'none';
        });
    }, 5000);

    // 角色选择功能
    document.querySelectorAll('.role-card').forEach(card => {
        card.addEventListener('click', function() {
            // 移除所有选中状态
            document.querySelectorAll('.role-card').forEach(c => {
                c.classList.remove('selected');
            });

            // 添加当前选中状态
            this.classList.add('selected');

            // 设置隐藏字段的值
            document.getElementById('roleInput').value = this.getAttribute('data-role');
        });
    });
});